﻿using System.Numerics;
using MediatR;
using Rabbit.Domain;
using Rabbit.ErCode;
using Rabbit.lnfarnum;
using Rabbit.Read.API.Dtos;
using Rabbit.Read.API.GetRepository.Commders;

namespace Rabbit.Read.API.GetRepository.CommdHenders
{
    /// <summary>
    /// /显示商品表
    /// </summary>
    public class ShowMerchandCommdHnder : IRequestHandler<ShowMerchandCommdes, APIResult<APIPing<MerchanDto>>>
    {

        private readonly IBaseRepository<Merchan> merchanRepository;

        private readonly IBaseRepository<MerchandiseMangem> merchandiseMangem;

        public ShowMerchandCommdHnder(IBaseRepository<Merchan> merchanRepository, IBaseRepository<MerchandiseMangem> merchandiseMangem)
        {
            this.merchanRepository = merchanRepository;
            this.merchandiseMangem = merchandiseMangem;
        }


         public Task<APIResult<APIPing<MerchanDto>>> Handle(ShowMerchandCommdes request, CancellationToken cancellationToken)
        {
            APIResult<APIPing<MerchanDto>> result = new APIResult<APIPing<MerchanDto>>();

            result.Code = ReCode.查询成功;
            result.Mage = ReCode.查询成功.ToString();

            var list = from a in merchanRepository.GetAll()
                       join b in merchandiseMangem.GetAll()
                       on a.MerchanMid equals b.MerchanMid
                       where b.Idate==false
                       select new MerchanDto
                       {
                           MerchandMangeid =b.MerchandMangeid,
                           MerchanMid = a.MerchanMid,
                           Pbatches = b.Pbatches,
                           Slfsky = b.Slfsky,
                           Plinumber = b.Plinumber,
                           ManufacName = b.ManufacName,
                           Coverimage = b.Coverimage,
                           Complete =b.Complete,
                           MerchanName =a.MerchanName,
                       };

            int TotaCount = list.Count();

            int Totapage = (int)Math.Ceiling(TotaCount * 1.0 / (request.PageISes));

            list = list.OrderByDescending(x => x.MerchandMangeid).Skip(request.PageISes * (request.PageIdez-1)).Take(request.PageISes);

            APIPing<MerchanDto> ping = new APIPing<MerchanDto>();

            ping.TotaCount = TotaCount;
            ping.Totapage = Totapage;
            ping.list =list.ToList();
            result.Data = ping;

            return  Task.FromResult(result);
        }
    }
}
